<template>
  <div class="login">
    <div class="logo">
      <img src="../assets/logo2.png" alt />
    </div>
    <van-form @submit="onSubmit">
      <div class="inp">
        <van-field v-model="username" placeholder="用户名" left-icon="manager" />
      </div>

      <div class="inp">
        <van-field v-model="password" placeholder="密码" type="password" left-icon="eye" />
      </div>
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">登录</van-button>
      </div>
      <div class="regist">
        <a href="#">忘记密码</a>
        <a href="#/zhuce">注册</a>
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: "",
      password: "",
      userType: "02"
    };
  },
  methods: {
    onSubmit(values) {
      var sb = /^[a-zA-Z][a-zA-Z0-9]{3,15}$/;
      if ("" == this.password) {
        this.$toast.fail("请输入密码");
      }
      if ("" == this.userName) {
        this.$toast.fail("请输入用户名");
      }
      if (!sb.test(this.password)) {
        this.$toast.fail("请输入4-16为密码，字母开头");
      } else {
        let paramForm = {
          username: this.username,
          password: this.password,
          userType: this.userType
        };
        this.$axios.post("prod-api/common/login", paramForm).then(res => {
          if (200 == res.data.code) {
            this.loginpPerson();
            localStorage.setItem("token", res.data.token);
            this.$router.push("/home");
            this.$toast.fail(res.data.msg);
          } else {
            (this.username = ""),
              (this.password = ""),
              this.$toast.fail(res.data.msg);
          }
        });
      }
    },
    loginpPerson() {
      let paramForms = {};
      this.$axios.get("prod-api/common/getLoginInfo", paramForms).then(res => {
        if (200 == res.data.code) {
          localStorage.setItem("userName", res.data.user.userName);
          this.role = res.data.roles[0];

          localStorage.setItem("userName", res.data.user.userName);
          if (this.role == "backend") {
            this.$router.push("/");
            this.$toast.fail("该用户无法登陆app端");
          }
        } else {
          this.$toast.fail(res.data.msg);
        }
      });
    }
  }
};
</script>

<style  scoped>
.logo img {
  position: absolute;
  left: 16%;
  top: 20%;
}
.logo {
  position: relative;
  width: 100%;
  height: 245px;
  background: url(../assets/bg1.png) no-repeat;
  background-size: 100%;
}
.inp {
  margin-left: 50px;
  margin-top: 25px;
}
.regist {
  margin-top: 30px;
  text-align: center;
}
.regist a {
  text-decoration: none;
  color: gray;
  font-size: 14px;
  margin-left: 45px;
  margin-right: 45px;
}

.van-cell {
  width: 250px;
  border-bottom: 2px solid #eee;
  color: #046ac6;
}
</style>